<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { Delete, Key } from "@element-plus/icons-vue";

import { checkService, deleteService, listByPageService } from "@/api/root/register";

// 导航栏
import { ComponentSize, ElMessage } from "element-plus";
const currentPage = ref(1); //当前页数
const pageSize = ref(7); //每页条数
const total = ref(100); //总条数

const size = ref<ComponentSize>("large");
const background = ref(true);
const disabled = ref(false);

const handleCurrentChange = (num) => {
  //页数改变触发函数
  currentPage.value = num;
  listByPage();
};

const accountSearch = ref(""); //账号-条件查询
const timeSearch = ref("");
let registerData = ref([]); //注册请求数据列表

//清空查询条件
const clearSearch = () => {
  accountSearch.value = "";
  timeSearch.value = "";
  listByPage();
};

//条件分页获取职位列表
const listByPage = async () => {
  let params = {
    currentPage: currentPage.value,
    pageSize: pageSize.value,
    account: accountSearch.value ? accountSearch.value : null,
    minTime: timeSearch.value[0],
    maxTime: timeSearch.value[1],
  };
  let result = await listByPageService(params);
  registerData.value = result.data.items;
  total.value = result.data.total;
};

listByPage();

//用户注册信息审核表单
const checkFormVisible = ref(false);
import type { FormInstance, FormRules } from "element-plus";

const ruleFormRef = ref<FormInstance>();
const validateRoot = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("权限不能设置为空"));
  } else {
    callback();
  }
};

const registerForm = ref({
  id: "",
  nickname: "",
  account: "",
  password: "",
  root: "",
});

const rules = ref<FormRules<typeof registerForm>>({
  root: [{ validator: validateRoot, trigger: "blur" }],
});

//清空表单模型
const clearForm = () => {
  ruleFormRef.value.resetFields();
  registerForm.value = {
    id: "",
    nickname: "",
    account: "",
    password: "",
    root: "",
  };
};

//审核提交函数
const check = async () => {
  await checkService(registerForm.value);
  ElMessage.success("审核通过");
};

//根据id删除信息函数
const deleteById = async (id) => {
  await deleteService(id);
  ElMessage.success("删除成功");
};
</script>

<template>
  <el-card style="min-width: 800px; width: 800px; margin: auto">
    <!-- 查询条件 -->
    <div style="display: flex; justify-content: space-around">
      <el-input
        v-model="accountSearch"
        style="width: 240px"
        placeholder="输入想要查询的账号"
        @input="listByPage()"
      />
      &nbsp;&nbsp;&nbsp;
      <!-- 时间条件 -->
      <el-date-picker
        v-model="timeSearch"
        type="daterange"
        range-separator="至"
        start-placeholder="想要查询的开始时间"
        end-placeholder="想要终止的开始时间"
        :size="size"
        @change="listByPage()"
      />
      &nbsp;&nbsp;&nbsp;
      <el-button style="height: 40px" type="info" @click="clearSearch()"
        >重置条件</el-button
      >
    </div>
    <el-table :data="registerData" style="width: 100%; height: 460px">
      <el-table-column prop="id" label="编号" width="144" />
      <el-table-column prop="createTime" label="申请时间" width="200" />
      <el-table-column prop="account" label="账号" width="144" />
      <el-table-column prop="password" label="密码" width="300" />
      <el-table-column fixed="right" label="操作" width="100">
        <template #default="{ row }">
          <el-button
            type="success"
            :icon="Key"
            circle
            @click="
              checkFormVisible = true;
              clearForm();
            "
          />
          <el-popconfirm
            title="确定要删除该数据吗？"
            @cancel="ElMessage('您取消了删除')"
            @confirm="deleteById(row.id)"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" circle />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 导航栏 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>

  <!-- 类型信息修改、新增表单 -->
  <el-dialog v-model="checkFormVisible" title="用户注册信息审核" width="500">
    <el-form :model="registerForm"> </el-form>
    <template #footer>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px; padding-left: 100px"
        :model="registerForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="昵称：" prop="name">
          <el-input
            v-model="registerForm.nickname"
            autocomplete="off"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="权限：" prop="money"
          ><el-select
            v-model="registerForm.root"
            placeholder="请选择权限"
            size="large"
            style="width: 200px"
          >
            <el-option label="普通权限" value="0" />
            <el-option label="管理员权限" value="1" />
          </el-select>
        </el-form-item>
      </el-form>

      <div class="dialog-footer">
        <el-button @click="checkFormVisible = false">取消审核</el-button>
        <el-button type="primary" @click="check()"> 审核通过 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
